//
//  ProductViewCell.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/11/20.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "ProductViewCell.h"
#import "UIImageView+AFNetworking.h"
#import "LYButton.h"
#import "UIViewExt.h"
//给view添加边框
#import <QuartzCore/QuartzCore.h>
#import "Common.h"

#define MENU_ITEM_SPACE     10
#define MENU_ITEM_WHIDE     ([[UIScreen mainScreen] bounds].size.width - 6*5)/3
#define MENU_ITEM_HEIGHT    50

#define MAX_ITEM_COUNT      3


@interface ProductViewCell()

//下拉菜单数据源
@property (nonatomic, strong) NSMutableArray *menuItemDataSourceArray;

//是否已经绘制了下拉菜单
@property (nonatomic, assign) BOOL isAlreadyDrawMenu;
@end




@implementation ProductViewCell




- (void)awakeFromNib {
    
    
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 99.5, ViewWidth,0.5)];
    lbl.backgroundColor = [UIColor colorWithRed:230.0/255 green:230.0/255 blue:244.0/255 alpha:1];  [self.contentView addSubview:lbl];
    
    
    
    self.menuView.layer.borderWidth = 0.3;
    self.menuView.layer.borderColor = [BACKGROUND2 CGColor];
    self.menuView.backgroundColor = [UIColor lightGrayColor];
//    self.btnGou.backgroundColor = [UIColor greenColor];
    self.lableSJ.hidden = YES;
    self.imageSJ = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40 /FDLaOt, 40 /FDLaOt)];
    [self.contentView addSubview:self.imageSJ];
//    self.imageSJ.contentMode = UIViewContentModeScaleAspectFill;
    
}







-(void)loadCell:(NSDictionary *)dic2 buttonTitle:(NSString *)title

{

    
    if ([title isEqualToString:@"编辑"]) {
        
        
        self.btn1.hidden = NO;
        //隐藏生成底部View按钮
        
    }
    
    else
    {
    
        self.btn1.hidden = YES;
    
    }
    
    
    if ([[dic2 objectForKey:@"image"] isKindOfClass:[NSNull class]] || [[dic2 objectForKey:@"image"] length] == 0 ) {
        self.image1.image = [UIImage imageNamed:@"backImage"];
    }
    
    NSURL *url = [ NSURL URLWithString:[dic2 objectForKey:@"image"]];
    [self.image1 setImageWithURL:url placeholderImage:[UIImage imageNamed:@"backImage"]];
    
    
    self.labelName.text = [dic2 objectForKey:@"product_name"];
    self.labelUnit.text = [NSString stringWithFormat:@"规  格:%@",[dic2 objectForKey:@"product_unit"]];
    self.labelPrice.text = [NSString stringWithFormat:@"￥%@",[dic2 objectForKey:@"unit_price"]];
    
    

    
    if ([[dic2 objectForKey:@"status"] isEqualToString:@"1"] ) {
        self.lableSJ.text = @"上架";
        self.imageSJ.image = [UIImage imageNamed:@"shangjia"];
    
    }
    else
    {
    
    self.lableSJ.text = @"下架";
        self.imageSJ.image = [UIImage imageNamed:@"shangjia2"];
    }




}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    //设置单元格菜单是否被打开，其实可以直接使用isSelected代替。
    self.isOpenMenu = selected;
}



/**
 *  下拉菜单按钮
 *
 *  @param sender 下拉菜单按钮
 */
- (IBAction)btn1:(UIButton *)sender
{
//    NSLog(@"666666");
    if ([self.delegate respondsToSelector:@selector(didOpenMenuAtCell:withMoreButton:)])
    {
        [self.delegate didOpenMenuAtCell:self withMoreButton:sender];
    }
    
}



/**
 *  下拉菜单item点击事件
 *
 *  @param sender 下拉菜单item
 */
- (void)menuItemClick:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(menuTableViewCell:didSeletedMentItemAtIndex:)])
    {
        [self.delegate menuTableViewCell:self didSeletedMentItemAtIndex:sender.tag];
    }
}



/**
 *  构建下拉视图
 */
- (void)buildMenuView
{
    
    
//    NSLog(@"uiouiuo");
    //避免多次绘制下拉菜单
    if (self.isAlreadyDrawMenu)
    {
        return;
    }
    
    //构建菜单
    self.menuItemDataSourceArray = [NSMutableArray arrayWithCapacity:0];
    if ([self.dataSource respondsToSelector:@selector(dataSourceForMenuItem)])
    {
        self.menuItemDataSourceArray = [self.dataSource dataSourceForMenuItem];
        
        __weak ProductViewCell *weakSelf = self;
        [self.menuItemDataSourceArray enumerateObjectsUsingBlock:^(MenuItemModel *menuItemModel, NSUInteger idx, BOOL *stop) {
            
            if (idx >= MAX_ITEM_COUNT)
            {
                //下拉菜单的item超过最大数(MAX_ITEM_COUNT:5)的时候就不绘制,可以自定义下拉菜单个数
                return ;
            }
            
            CGRect menuItemRect = CGRectMake(MENU_ITEM_SPACE + (MENU_ITEM_SPACE + MENU_ITEM_WHIDE) * idx, 0, MENU_ITEM_WHIDE, MENU_ITEM_HEIGHT);
            LYButton *menuItemButton = [[LYButton alloc] initWithFrame:menuItemRect model:menuItemModel];
            menuItemButton.tag = idx;
            [menuItemButton addTarget:self
                               action:@selector(menuItemClick:)
                     forControlEvents:UIControlEventTouchUpInside];
            [weakSelf.menuView addSubview:menuItemButton];
        }];
    }
    
    self.isAlreadyDrawMenu = YES;
}

- (IBAction)buttonGOu:(id)sender {
}
@end
